package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.OptionBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.facebook.stetho.common.*;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvzhihao on 17-4-29.
 */

public class SingleChooseUtil {


    public static void getChooseDialog(BaseActivity activity, int option, OnGetDialog onGetDialog, OnSingChooseListener onSingChooseListener) {
        OkGo.<List<OptionBean>>get(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .params("type", option)
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS + option)
                .execute(new DialogCallBack<List<OptionBean>>(activity) {
                    @Override
                    public void onSuccess(Response<List<OptionBean>> response) {
                        List<OptionBean> optionBean = response.body();
                        ArrayList<String> strings = new ArrayList<>();
                        for (OptionBean optionsBean : optionBean) {
                            strings.add(optionsBean.getName());
                        }
                        ActionSheetDialog actionSheetDialog = ChooseAlertView.getBottomChoose(activity, "请选择", strings, (v, p) -> {
                            onSingChooseListener.onSingChooseListener(p, strings);

                        });
                        onGetDialog.onGetDialog(actionSheetDialog);
                    }

                    @Override
                    public void onCacheSuccess(Response<List<OptionBean>> response) {
                        super.onCacheSuccess(response);
                        List<OptionBean> optionBean = response.body();
                        ArrayList<String> strings = new ArrayList<>();
                        for (OptionBean optionsBean : optionBean) {
                            strings.add(optionsBean.getName());
                        }
                        ActionSheetDialog actionSheetDialog = ChooseAlertView.getBottomChoose(activity, "请选择", strings, (v, p) -> {
                            onSingChooseListener.onSingChooseListener(p, strings);

                        });
                        onGetDialog.onGetDialog(actionSheetDialog);
                    }

                    @Override
                    public void onError(Response<List<OptionBean>> response) {
                        super.onError(response);
                        ToastUtil.showShort("数据获取失败，请稍候重试");
                    }
                });
    }

    public static void getChooseDialog(BaseActivity activity, List list, String field, OnGetDialog onGetDialog, OnSingChooseListener onSingChooseListener) {

        if (ListUtil.isEmpty(list)) {
            System.out.println("列表为空");
            return;
        }
        ArrayList<String> strings = new ArrayList<>();

        if (list.get(0) instanceof String) {
            strings = (ArrayList<String>) list;
        } else {

        }
        try {
            for (Object o : list) {
                strings.add(getField(o, field));
            }
        } catch (Exception e) {
            System.out.println("SingleChooseUtil->getChooseDialog 异常");
            e.printStackTrace();
        }
        ArrayList<String> finalStrings = strings;
        ActionSheetDialog actionSheetDialog = ChooseAlertView.getBottomChoose(activity, "请选择", strings, (v, p) -> {
            onSingChooseListener.onSingChooseListener(p, finalStrings);

        });
        onGetDialog.onGetDialog(actionSheetDialog);

    }

    public interface OnSingChooseListener {
        void onSingChooseListener(int position, List<String> strings);
    }

    public interface OnGetDialog {
        void onGetDialog(ActionSheetDialog sheetDialog);
    }


    private static String getField(Object o, String field) throws Exception {
        Field declaredField = o.getClass().getDeclaredField(field);
        declaredField.setAccessible(true);
        return (String) declaredField.get(o);
    }
}
